//<MStar Software>
//******************************************************************************
// MStar Software
// Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
// All software, firmware and related documentation herein ("MStar Software") are
// intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
// law, including, but not limited to, copyright law and international treaties.
// Any use, modification, reproduction, retransmission, or republication of all
// or part of MStar Software is expressly prohibited, unless prior written
// permission has been granted by MStar.
//
// By accessing, browsing and/or using MStar Software, you acknowledge that you
// have read, understood, and agree, to be bound by below terms ("Terms") and to
// comply with all applicable laws and regulations:
//
// 1. MStar shall retain any and all right, ownership and interest to MStar
//    Software and any modification/derivatives thereof.
//    No right, ownership, or interest to MStar Software and any
//    modification/derivatives thereof is transferred to you under Terms.
//
// 2. You understand that MStar Software might include, incorporate or be
//    supplied together with third party's software and the use of MStar
//    Software may require additional licenses from third parties.
//    Therefore, you hereby agree it is your sole responsibility to separately
//    obtain any and all third party right and license necessary for your use of
//    such third party's software.
//
// 3. MStar Software and any modification/derivatives thereof shall be deemed as
//    MStar's confidential information and you agree to keep MStar's
//    confidential information in strictest confidence and not disclose to any
//    third party.
//
// 4. MStar Software is provided on an "AS IS" basis without warranties of any
//    kind. Any warranties are hereby expressly disclaimed by MStar, including
//    without limitation, any warranties of merchantability, non-infringement of
//    intellectual property rights, fitness for a particular purpose, error free
//    and in conformity with any international standard.  You agree to waive any
//    claim against MStar for any loss, damage, cost or expense that you may
//    incur related to your use of MStar Software.
//    In no event shall MStar be liable for any direct, indirect, incidental or
//    consequential damages, including without limitation, lost of profit or
//    revenues, lost or damage of data, and unauthorized system use.
//    You agree that this Section 4 shall still apply without being affected
//    even if MStar Software has been modified by MStar in accordance with your
//    request or instruction for your use, except otherwise agreed by both
//    parties in writing.
//
// 5. If requested, MStar may from time to time provide technical supports or
//    services in relation with MStar Software to you for your use of
//    MStar Software in conjunction with your or your customer's product
//    ("Services").
//    You understand and agree that, except otherwise agreed by both parties in
//    writing, Services are provided on an "AS IS" basis and the warranty
//    disclaimer set forth in Section 4 above shall apply.
//
// 6. Nothing contained herein shall be construed as by implication, estoppels
//    or otherwise:
//    (a) conferring any license or right to use MStar name, trademark, service
//        mark, symbol or any other identification;
//    (b) obligating MStar or any of its affiliates to furnish any person,
//        including without limitation, you and your customers, any assistance
//        of any kind whatsoever, or any information; or
//    (c) conferring any license or right under any intellectual property right.
//
// 7. These terms shall be governed by and construed in accordance with the laws
//    of Taiwan, R.O.C., excluding its conflict of law rules.
//    Any and all dispute arising out hereof or related hereto shall be finally
//    settled by arbitration referred to the Chinese Arbitration Association,
//    Taipei in accordance with the ROC Arbitration Law and the Arbitration
//    Rules of the Association by three (3) arbitrators appointed in accordance
//    with the said Rules.
//    The place of arbitration shall be in Taipei, Taiwan and the language shall
//    be English.
//    The arbitration award shall be final and binding to both parties.
//
//******************************************************************************
//<MStar Software>

package com.mstar.android.media;

import android.util.Log;
import java.net.IDN;
import android.os.Parcel;

/**
 * Class to hold the audio's information. AudioTrackInfo are used for human
 * consumption and can be embedded in the media (e.g shoutcast) or available
 * from an external source. The source can be local (e.g thumbnail stored in the
 * DB) or remote (e.g caption server). NOTE:Users are only allowed to use the
 * getXXX function to get the information they need. Can't ues this class
 * alone.Make sure that you have called getAudioTrackInfo function in
 * mediaplayer.java before use these interfaces.
 */
public class AudioTrackInfo {
    private final static String TAG = "AudioTrackInfo";

    private int mBitRate;

    private int mTotalPlayTime;

    private int mCurrentPlayTime;

    private int mSampleRate;

    private int mCodecID;

    private int mYear;

    private int mCurrentAudioTrackID;

    private String mTitle;

    private String mAlbum;

    private String mArtist;

    private String mAudioCodec;
    /**
     * The following language types for audio must be synchronized with the enum
     * of EN_MEDIA_AUDIO_LANGUAGE in MstMetadata.h file
     */
    private enum MEDIA_AUDIO_LANGUAGE {
        // / GERMAN
        E_MEDIA_AUDIO_LANGUAGE_GERMAN,
        // / ENGLISH
        E_MEDIA_AUDIO_LANGUAGE_ENGLISH,
        // / SPANISH
        E_MEDIA_AUDIO_LANGUAGE_SPANISH,
        // / GREEK
        E_MEDIA_AUDIO_LANGUAGE_GREEK,
        // / FRENCH
        E_MEDIA_AUDIO_LANGUAGE_FRENCH,
        // / CROATIAN
        E_MEDIA_AUDIO_LANGUAGE_CROATIAN,
        // / ITALIAN
        E_MEDIA_AUDIO_LANGUAGE_ITALIAN,
        // / DUTCH
        E_MEDIA_AUDIO_LANGUAGE_DUTCH,
        // / POLISH
        E_MEDIA_AUDIO_LANGUAGE_POLISH,
        // / PORTUGUESE
        E_MEDIA_AUDIO_LANGUAGE_PORTUGUESE,
        // / RUSSIAN
        E_MEDIA_AUDIO_LANGUAGE_RUSSIAN,
        // / ROMANIAN
        E_MEDIA_AUDIO_LANGUAGE_ROMANIAN,
        // / SWEDISH
        E_MEDIA_AUDIO_LANGUAGE_SWEDISH,
        // / ARABIC
        E_MEDIA_AUDIO_LANGUAGE_ARABIC,
        // / CHINESE
        E_MEDIA_AUDIO_LANGUAGE_CHINESE,
        // / JAPANESE
        E_MEDIA_AUDIO_LANGUAGE_JAPANESE,
        // / KOREAN
        E_MEDIA_AUDIO_LANGUAGE_KOREAN,
        // / Invalid.
        E_MEDIA_AUDIO_LANGUAGE_UNDEFINED,
    };

    private MEDIA_AUDIO_LANGUAGE mLanguageType;

    public AudioTrackInfo(boolean typeIsAudio, Parcel p, String[] strInfo) {
        int bitRate = p.readInt();
        String year = p.readString();
        int sampleRate = p.readInt();
        int totalPlayTime = p.readInt();
        int currentPlayTime = p.readInt();
        int codecID = p.readInt();
        int audioLangIndex = p.readInt();
        int currentAudioTrackID = p.readInt();
        String audioCodec = p.readString();

        if (typeIsAudio) {
            mBitRate = bitRate;
            if (!"".equals(year)) {
                String theYear = IDN.toUnicode(year);
                int len = theYear.length();
                if (0 == len) {
                    Log.d(TAG, "The year information is null.");
                    mYear = 0;
                } else {
                    String date = "";
                    for (int i = 0; i < len; i++) {
                        char curChar = theYear.charAt(i);

                        if (curChar >= '0' && curChar <= '9') {
                            date = date.concat(String.valueOf(curChar));
                        } else if ((i + 2) >= len
                                || (theYear.charAt(i + 2) <= '0' || theYear.charAt(i + 2) >= '9')) {
                            // eg:"2012-1-1" -> 20120101 "2012-10-10 0" ->
                            // 20121010
                            date = date.concat("0");
                        }
                    }
                    int length = 8;

                    if (length > date.length()) {
                        length = date.length();
                    }

                    date = date.substring(0, length);
                    mYear = Integer.parseInt(date);
                    Log.d(TAG, "mYear:" + mYear);
                }
            } else {
                mYear = 0;
            }
            mSampleRate = sampleRate;
            mTitle = strInfo[0];
            mAlbum = strInfo[1];
            mArtist = strInfo[2];
            mTotalPlayTime = -1;
            mCurrentPlayTime = -1;
        } else {
            mTotalPlayTime = totalPlayTime;
            mCurrentPlayTime = currentPlayTime;
            mBitRate = -1;
            mYear = 0;
            mSampleRate = -1;
            mTitle = null;
            mAlbum = null;
            mArtist = null;
        }
        mCodecID = codecID;
        mLanguageType = MEDIA_AUDIO_LANGUAGE.values()[audioLangIndex];
        mCurrentAudioTrackID = currentAudioTrackID;
        mAudioCodec = audioCodec;
    };

    /**
     * Gets the audio encoding format's id. Note:It is not the track number.
     */
    public int getCodecID() {
        return mCodecID;
    }

    /**
     *Gets the audio codec type.
     */
    public String getAudioCodec() {
        return mAudioCodec;
    }

    /**
     * Gets current audio track id.
     */
    public int getCurrentAudioTrackID() {
        return mCurrentAudioTrackID;
    }

    /**
     * Gets the total play time. Only vedio type can get this value.
     */
    public int getTotalPlayTime() {
        if (-1 == mTotalPlayTime) {
            Log.d(TAG, "Only vedio type can get TotalPlayTime.");
        }
        return mTotalPlayTime;
    }

    /**
     * Gets the current play time. Only vedio type can get this value.
     */
    public int getCurrentPlayTime() {
        if (-1 == mCurrentPlayTime) {
            Log.d(TAG, "Only vedio type can get CurrentPlayTime.");
        }
        return mCurrentPlayTime;
    }

    /**
     * Gets the bite rate. Only audio type can get this value.
     */
    public int getBitRate() {
        if (-1 == mBitRate) {
            Log.d(TAG,
                    "Only audio type can get BiteRate and local audio couldn't get infomation with the function.");
        }
        return mBitRate;
    }

    /**
     * Gets the release year. Only audio type can get this value.
     */
    public int getYear() {
        if (0 == mYear) {
            Log.d(TAG,
                    "Only audio type can get Year and local audio couldn't get infomation with the function.");
        }
        return mYear;
    }

    /**
     * Gets the sample rate. Only audio type can get this value.
     */
    public int getSampleRate() {
        if (-1 == mSampleRate) {
            Log.d(TAG,
                    "Only audio type can get SampleRate and local audio couldn't get infomation with the function.");
        }
        return mSampleRate;
    }

    /**
     * Gets the audio's title. Only audio type can get this value.
     */
    public String getTitle() {
        if (null == mTitle) {
            Log.d(TAG,
                    "Only audio type can get Title and local audio couldn't get infomation with the function.");
        }
        return mTitle;
    }

    /**
     * Gets the audio's album. Only audio type can get this value.
     */
    public String getAlbum() {
        if (null == mAlbum) {
            Log.d(TAG,
                    "Only audio type can get Album and local audio couldn't get infomation with the function.");
        }
        return mAlbum;
    }

    /**
     * Gets the audio's artist. Only audio type can get this value.
     */
    public String getArtist() {
        if (null == mArtist) {
            Log.d(TAG,
                    "Only audio type can get Artist and local auido couldn't get infomation with the function.");
        }
        return mArtist;
    }

    /**
     * Gets the audio's language type.
     */
    public String getAudioLanguageType() {
        if (mLanguageType == null) {
            return null;
        }

        String audioLanguageType;
        switch (mLanguageType) {
            case E_MEDIA_AUDIO_LANGUAGE_GERMAN:
                audioLanguageType = "German";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_ENGLISH:
                audioLanguageType = "English";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_SPANISH:
                audioLanguageType = "Spanish";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_GREEK:
                audioLanguageType = "Greek";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_FRENCH:
                audioLanguageType = "French";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_CROATIAN:
                audioLanguageType = "Croatian";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_ITALIAN:
                audioLanguageType = "Italian";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_DUTCH:
                audioLanguageType = "Dutch";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_POLISH:
                audioLanguageType = "Polish";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_PORTUGUESE:
                audioLanguageType = "Portuguese";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_RUSSIAN:
                audioLanguageType = "Russian";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_ROMANIAN:
                audioLanguageType = "Romanian";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_SWEDISH:
                audioLanguageType = "Swedish";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_ARABIC:
                audioLanguageType = "Arabic";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_CHINESE:
                audioLanguageType = "Chinese";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_JAPANESE:
                audioLanguageType = "Japanese";
                break;
            case E_MEDIA_AUDIO_LANGUAGE_KOREAN:
                audioLanguageType = "Korean";
                break;
            default:
                audioLanguageType = "undefined";
                break;
        }

        return audioLanguageType;
    }
}
